package com.ibm.cps.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.storm.guava.base.Throwables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ibm.cps.model.ServletUtil;
import com.ibm.util.ErrorCode;
import com.ibm.util.JSONUtility;
import com.ibm.util.exception.CPSException;

public class Publishers extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private Logger logger;
	private ServletTemplate publisherImpl;

	@Override
	public void init() throws ServletException {
		logger = LoggerFactory.getLogger(this.getClass());
		try {
			publisherImpl = new PublisherImplement();
		} catch (CPSException e) {
			logger.error(Throwables.getStackTraceAsString(e));
		}
	}

	@Override
	protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String retMsg = ServletUtil.constructReturnMsg(ErrorCode.INVALID_REQUEST_CODE, "Default error");
		try {
			Map<String, String> key2Value = ServletUtil.getParameters(req);
			String input = ServletUtil.getInputArguments(req.getInputStream());
			if (input != null && input.length() >= 1) {
				HashMap<String, String> jsonKeyvalue = JSONUtility.getKeyValue(input);
				key2Value.putAll(jsonKeyvalue);
			}
			retMsg = publisherImpl.doDelete(key2Value);
		} catch (Throwable e) {

			retMsg = ServletUtil.getException(e);
		} finally {
			PrintWriter writer = resp.getWriter();
			writer.write(retMsg);
			writer.close();
		}
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String retMsg = ServletUtil.constructReturnMsg(ErrorCode.INVALID_REQUEST_CODE, "Default error");
		try {
			Map<String, String> key2Value = ServletUtil.getParameters(req);
			retMsg = publisherImpl.doGet(key2Value);
		} catch (Throwable e) {

			retMsg = ServletUtil.getException(e);
		} finally {
			PrintWriter writer = resp.getWriter();
			writer.write(retMsg);
			writer.close();
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String retMsg = ServletUtil.constructReturnMsg(ErrorCode.INVALID_REQUEST_CODE, "Default error");
		try {
			Map<String, String> key2Value = ServletUtil.getParameters(req);
			String input = ServletUtil.getInputArguments(req.getInputStream());
			retMsg = publisherImpl.doPost(key2Value, input);
		} catch (Throwable e) {
			logger.error(Throwables.getStackTraceAsString(e));
			retMsg = ServletUtil.getException(e);
		} finally {
			PrintWriter writer = resp.getWriter();
			writer.write(retMsg);
			writer.close();
		}
	}

	@Override
	protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String retMsg = ServletUtil.constructReturnMsg(ErrorCode.INVALID_REQUEST_CODE, "Default error");
		try {
			Map<String, String> key2Value = ServletUtil.getParameters(req);
			String input = ServletUtil.getInputArguments(req.getInputStream());
			retMsg = publisherImpl.doPut(key2Value, input);
		} catch (Throwable e) {
			logger.error(Throwables.getStackTraceAsString(e));
			retMsg = ServletUtil.getException(e);
		} finally {
			PrintWriter writer = resp.getWriter();
			writer.write(retMsg);
			writer.close();
		}
	}
}